#!/bin/sh
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/hadoop/bin:/root/bin:/usr/bin:/usr/sbin:/sbin:/bin:.
export LC_ALL=en_US.UTF-8
umask 0022

logdir=$1
hostlist=$2

# avoid ping outside website and localhost
ping_host_list=$(cat /etc/hosts|grep "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"|grep -v '127.0.0.1'|grep -v "com\|net\|org\|cn\|gov\|org\|biz\|top\|pub\|info\|edu\|int\|pro\|name\|mil$" | head -n 8) 

if [ "x${hostlist}" != "x" ];then
	# read ping host from file
	if [ -f ${hostlist} ]; then
		ping_host_list=$(cat ${hostlist})
	fi
fi

echo "${ping_host_list}" | while read line
do
	ip=$(echo $line|awk '{print $1}')
	host=$(echo $line|awk '{print $2}')
	if [ "x${ip}" != "x" ] && [ "x${host}" != "x" ]; then
		logname=${logdir}/ping_${ip}_${host}.log.$(date '+%Y%m%d')
		nohup ping -D -W 1 ${ip} >> ${logname} 2>&1 &
	fi
done 